<?php

namespace app\admin\controller;

use app\common\model\Menu as MenuModel;
use app\common\model\AuthGroup as GroupModel;
use app\common\model\AdminUser as AdminModel;
use think\Controller;
use think\request;

use app\admin\service\TreeService;



class Base extends Controller
{
    public $http;

    public function __construct(TreeService $service, AdminModel $data, request $request)
    {
        parent::__construct();
        $username = session("user_session.username");
        $profile_data = $data->where(array('id' => session("user_session.uid")))->find();
        $profile_data['avatar'] = "http://" . $_SERVER['HTTP_HOST'] . $profile_data['avatar'];
        $route = $request->url();
        $str = preg_replace(array('/\/admin/', '/\/[0-9]/', '/\.html/'), '', $route, 1);
        $menuId = MenuModel::where(array('jump_url' => $str))->value('id');
        if ($username == 'admin') {
            session('menu_auth', 'all');
        } else {
            $menuAuth = GroupModel::where(array('id' => $profile_data['group_id']))->value('menu_auth');
            $list = explode(',', $menuAuth);
            session('menu_auth', $menuAuth);
            if (!in_array($menuId, $list)) {
                return $this->error('抱歉，你没有该操作的权限！');
            }
        }
        $menuTree = $service->getMenuTree();
        $menuHtml = $service->getMenuHtml($menuTree);
        $this->assign('menu', $menuHtml);
        $this->assign('username', $username);
        $this->assign('profile_data', $profile_data);
    }
}